package com.frame.compiler.widget.permission;

import android.content.Context;
import android.util.Log;

import com.frame.compiler.widget.ToastUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

//权限申请帮助类
public class PermissionHelper {

    //申请单个权限的时候用到
    public void requestPermission(Context context, final onPermissionListener onPermissionListener, String permission) {
        XXPermissions xxPermissions = XXPermissions.with(context);
        //申请单个权限
        xxPermissions.permission(permission);

        XXPermissions.with(context)
                .permission(permission)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            onPermissionListener.onSuccess();
                        } else {
//                    ToastUtils.show("获取部分权限成功，但部分权限未正常授予");
                            onPermissionListener.onNoAllSuccess();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.show("被永久拒绝授权，请到系统设置页面手动授予权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                    XXPermissions.startPermissionActivity(context, denied);
                        } else {
                            if (permissions.contains(Permission.ACCESS_BACKGROUND_LOCATION)) {
                                ToastUtils.show("没有授予后台定位权限，请您选择\"始终允许\"");
                            } else {
                                ToastUtils.show("获取权限失败");
                            }
                        }

                        onPermissionListener.onFail();
                    }
                });
    }

    //申请多个权限的时候用到
    public void requestPermission(Context context, final onPermissionListener onPermissionListener, List<String> permissions, String[]... permission) {
        XXPermissions xxPermissions = XXPermissions.with(context);
        if (permissions != null && !permissions.isEmpty()) {
            //申请单个权限
            xxPermissions.permission(permissions);
        }
//        Log.e("requestPermission","permission.length==="+permission.length);
        if (permission.length != 0) {
            //申请权限组
            xxPermissions.permission(permission);
        }

        xxPermissions.request(new OnPermissionCallback() {
            @Override
            public void onGranted(List<String> permissions, boolean all) {
                if (all) {
                    onPermissionListener.onSuccess();
                } else {
//                    ToastUtils.show("获取部分权限成功，但部分权限未正常授予");
                    onPermissionListener.onNoAllSuccess();
                }
            }

            @Override
            public void onDenied(List<String> permissions, boolean never) {
                if (never) {
                    ToastUtils.show("被永久拒绝授权，请到系统设置页面手动授予权限");
                    // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                    XXPermissions.startPermissionActivity(context, denied);
                } else {
                    if (permissions.contains(Permission.ACCESS_BACKGROUND_LOCATION)) {
                        ToastUtils.showLong("没有授予后台定位权限，请您选择\"始终允许\"");
                    } else {
                        ToastUtils.show("获取权限失败");
                    }
                }

                onPermissionListener.onFail();
            }
        });
    }

    //判断是否具有定位的权限
    public boolean hasHomePermission(Context context) {
        return XXPermissions.isGranted(context, Permission.ACCESS_COARSE_LOCATION,Permission.ACCESS_FINE_LOCATION);
    }

    public interface onPermissionListener {
        void onSuccess();

        void onNoAllSuccess();

        void onFail();
    }
}
